package com.gsafety.library.integration;

import android.app.Application;
import android.content.Context;

import com.gsafety.library.LifeCycle.ActivityLifeCycle;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : lj
 * @date : 2020/9/16 17:26
 */
public class AppDelegate implements AppLifeCycles {
    private List<ConfigModule> configList;
    private List<AppLifeCycles> appLifeList;


    public AppDelegate(Context context) {
        configList = new ManifestParser(context).parse();
        appLifeList = new ArrayList<>();
        ManifestParser manifestParser = new ManifestParser(context);
        configList = manifestParser.parse();
        if (configList != null && configList.size() > 0) {
            for (ConfigModule configModule : configList) {
                configModule.injectAppLifecycle(context, appLifeList);
            }
        }
    }

    @Override
    public void attachBaseContext(Context base) {
        if (appLifeList != null && appLifeList.size() > 0) {
            for (AppLifeCycles life : appLifeList) {
                life.attachBaseContext(base);
            }
        }
    }

    @Override
    public void onCreate(Application application) {
        application.registerActivityLifecycleCallbacks(new ActivityLifeCycle());
        if (appLifeList != null && appLifeList.size() > 0) {
            for (AppLifeCycles life : appLifeList) {
                life.onCreate(application);
            }
        }

    }



}
